#include "DiolObject.h"

#include <assert.h>
#include "DiolButton.h"
#include "DiolAxis.h"
#include "DiolPOV.h"
#include "DiolDevice.h"

namespace Diol
{

Object::Object( const ObjectInstance& objectInstance, Device* parentDevice )
	: mObjectInstance(objectInstance),
	  mParentDevice(parentDevice)
{
	assert(mParentDevice);
}

Object::~Object()
{
}

bool Object::setUserData( UINT_PTR data )
{
	IDirectInputDevice8* inputDevice = getParentDevice()->getInputDevice();
	assert(inputDevice);

	DIPROPPOINTER diptr;
	diptr.diph.dwSize       = sizeof(DIPROPPOINTER);
	diptr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
	diptr.diph.dwHow        = DIPH_BYID;
	diptr.diph.dwObj        = getObjectInstance().getDwType();
	diptr.uData             = data;

	HRESULT hr = 0;
	hr = inputDevice->SetProperty( DIPROP_APPDATA, &diptr.diph );
	return SUCCEEDED(hr);
}

Object*	Object::createObject( const ObjectInstance& objectInstance, Device* parentDevice )
{
	if ( objectInstance.isAxis() )
		return new Axis( objectInstance, parentDevice );
	else if ( objectInstance.isButton() )
		return new Button( objectInstance, parentDevice );
	else if ( objectInstance.isPOV() )
		return new POV( objectInstance, parentDevice );
	return NULL;
}

void Object::notifyChanged()
{
	getParentDevice()->notifyObjectChanged(this);
}
}